//跳转到明细页面
var gotoOverSpeedDetailsPage;
//跳转到线路明细页面
var goLineInfo;
require(['grid', 'topBar', 'highcharts', 'date'], function () {
    var postData,rcx,type,orgIds;
    $('#top').topBar({
        left: [{
            type: 'filter-more',
            search: function () {
                search();
            },
            item: [
                {
                    text: '时间快捷',
                    name1: 'beginTime',
                    name2: 'endTime',
                    type: 'dateLine',
                    hidden:true
                }, 
                {
                    text: '按条件汇总',
                    name: 'sumTimeType',
                    type: 'item-select',
                    data: [
                        {text: '按日汇总', value: '10'},
                        {text: '按月汇总', value: '7'},
                        {text: '总体汇总', value: ''}
                    ]
                }
            ]
        }
        ],
        right: [
            {
                text: '导出',
                type: 'export-btn',
                authority: '',
                item: [
                    {
                        text: 'Excel导出',
                        click: function () {
                            exportFile('Excel');
                        }
                    },
                    {
                        text: 'PDF导出',
                        click: function () {
                            exportFile('Pdf');
                        }
                    }
                ]
            }
        ]
    });

    //获取左侧选择的节点信息
    function getPostData() {
        rcx = commonFun.getTreeCheckNodeToArgs();
        type = commonFun.getLeftTreeCurrentTreeType();
		orgIds = commonFun.getLeftTreeCheckedNodes(type);
        if (rcx) {
            postData = $.extend(rcx, $("#top").parent().getValues());
            postData.type = type;
            postData.orgIds = orgIds;
        }
        return rcx;
    }

    function exportFile(suffix) {
        if (getPostData()) {
            if ($('#grid').getGridParam("reccount") < 1) {
                $.messager.alert("提示", "请查询数据。", "info");
                return;
            }
            var url = "/overspeed/exportStatis" + suffix + ".htm";
            $.exportFile(url, postData);
        }
        if ($('#grid').getGridParam("reccount") > 0) {
            if (getPostData()) {
                var url = "/overspeed/exportStatis" + suffix + ".htm";
                $.exportFile(url, postData);
            }
        }
    }
    //grid初始化
    /*function gridInit() {*/
    $('#grid').parent().height($(window).height()-60);
	    $('#grid').grid({
	        multiselect: false,
	        url: "/overspeed/searchOverspeedStatisticsData.htm",
	        colModel: [
	            {name: "organId", label: "organId", hidden: true},
	            {name: "lineId", label: "lineId", hidden: true},
				{name: "ranking", label: "排名", align: "right",width:40, sortable:true},
	            {name: "organName", label: "所属机构",width:80},
	            {name: "lineName", label: "运营线路",width:80,formatter: 'customCurrency',formatoptions:{
	                redbtn:true,
	                css:'text-link c-blue',
	                clickFn:function(rowObject,cellvalue){
	                	goLineInfo(rowObject.lineId);
	                }
	            }},
	            {name: "operDate", label: "运营日期", align: "right",width:80},
	            {name: "spedCarCnt", label: "超速车台(台)", align: "right",width:80, index:"sped_Car_Cnt",
					formatter:'customCurrency',formatoptions:{
						redbtn:true,
						css:'text-link c-blue',
						clickFn:function(rowObject){
							var spedType = '';
							gotoOverSpeedDetailsPage(rowObject.organId, rowObject.lineId, spedType, rowObject.operDate);
						}
					}, sortable:true},
	            {name: "spedCnt", label: "超速次数(次)", align: "right",width:80, index:"sped_Cnt",
					formatter:'customCurrency',formatoptions:{
						redbtn:true,
						css:'text-link c-blue',
						clickFn:function(rowObject){
							var spedType = '';
							gotoOverSpeedDetailsPage(rowObject.organId, rowObject.lineId, spedType, rowObject.operDate);
						}
					}, sortable:true},
				{name: "warningCnt", label: "警告超速(次)", align: "right",width:80,formatter:'customCurrency',index:"warning_Cnt",
					formatoptions:{
					redbtn:true,
					css:'text-link c-blue',
					clickFn:function(rowObject){
						var spedType = 'PARA0101';
						gotoOverSpeedDetailsPage(rowObject.organId, rowObject.lineId, spedType, rowObject.operDate);
					}
				}, sortable:true},
	            {name: "seriousCnt", label: "严重超速(次)", align: "right",width:80,index:"serious_Cnt",
					formatter:'customCurrency',formatoptions:{
						redbtn:true,
						css:'text-link c-blue',
						clickFn:function(rowObject){
							var spedType = 'PARA0103';
							gotoOverSpeedDetailsPage(rowObject.organId, rowObject.lineId, spedType, rowObject.operDate);
						}
					}, sortable:true},
	            {name: "commonCnt", label: "一般超速(次)", align: "right",width:80,index:"common_Cnt",
					formatter:'customCurrency',formatoptions:{
						redbtn:true,
						css:'text-link c-blue',
						clickFn:function(rowObject){
							var spedType = 'PARA0102';
							gotoOverSpeedDetailsPage(rowObject.organId, rowObject.lineId, spedType, rowObject.operDate);
						}
					}, sortable:true},
	            {name: "otherWarningCnt", label: "其他警告(次)", align: "right",width:80, index:"other_Warning_Cnt",
	            	formatter:'customCurrency',formatoptions:{
						redbtn:true,
						css:'text-link c-blue',
						clickFn:function(rowObject){
							var spedType = 'PARA0104';
							gotoOverSpeedDetailsPage(rowObject.organId, rowObject.lineId, spedType, rowObject.operDate);
						}
	            	}, sortable:true
	            }
	        ]/*,
	        height:$(window).height()-459*/
	    });
    /*}*/

    function search() {
        if (getPostData()) {
        	$('#grid').grid('setGridParam', {
                page: 1,
                datatype: "json",
                postData: postData
            }).trigger("reloadGrid");
            $.ajaxExt({
				url: "/overspeed/searchOverspeedStatisticsList.htm",
				data:postData
			}).done(function(data){
				if(!data){
					$('#chart').parent().hide();
					$('#grid').jqGrid('clearGridData',true);
					return;
				}
				$('#chart').parent().show();
				var userData={lineName:'总计',spedCarCnt:0,spedCnt:0,seriousCnt:0,commonCnt:0,warningCnt:0,otherWarningCnt:0};
				$.each(data,function(index,item){
					for(var prop in userData){
						if(typeof userData[prop] !=='string'){
							userData[prop]+=item[prop];
						}
					}
				});
				// 展示报表
				showSpedStatisticsChart(userData.seriousCnt,userData.commonCnt,userData.warningCnt,userData.otherWarningCnt);
			});
        }
    }

    // -- 显示报表
	function showSpedStatisticsChart(seriousCnt,commonCnt,warningCnt,otherWarningCnt){
		$('#chart').highcharts({
			chart: {
				plotBackgroundColor: null,
				plotBorderWidth: null,
				plotShadow: false
			},
			title: {
				text: ''
			},
			credits: {
				enabled: false
			},
			plotOptions: {
				pie: {
					allowPointSelect: true,
					cursor: 'pointer',
					dataLabels: {
						enabled: true,
						inside: true,
						format: '<b>{point.name}</b>: {y} 次',
						style: {
							color: (Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black'
						}
					},
					distance: -30,
					showInLegend: true
				}
			},
			tooltip: {
				pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
			},
			legend: {
				layout: 'vertical',
				align: 'right',
				verticalAlign: 'middle',
				borderWidth: 0
			},
			series: [
				{
					type: 'pie',
					name: '超速次数',
					data: [
						{
							name:'严重',
							y:seriousCnt,
							color:'red'
						},
						{
							name:'一般',
							y:commonCnt,
							color:'orange'
						},
						{
							name:'警告',
							y:warningCnt,
							color:'yellow'
						},
						{
							name:'其他警告',
							y:otherWarningCnt,
							color:'#00FFFF'
						}
					]
				}
			]
		});

		var $labelLine = $('#chart').find('.highcharts-data-labels').find('path,shape');
		if(seriousCnt <= 0){
			$labelLine.eq(0).hide();
		}
		if(commonCnt <= 0){
			$labelLine.eq(1).hide();
		}
		if(warningCnt <= 0){
			$labelLine.eq(2).hide();
		}
		if(otherWarningCnt <= 0){
			$labelLine.eq(3).hide();
		}
	}

    //跳转到明细页面
    gotoOverSpeedDetailsPage = function (organId, lineId, spedType, operDate) {
    	var data = $("#top").parent().getValues();
		var type = commonFun.getLeftTreeCurrentTreeType();
		var orgIds = commonFun.getLeftTreeCheckedNodes(type);
		var statisBeginTime = "";
		var statisEndTime = "";
		if(operDate.length == 7){ // 按月汇总
			statisBeginTime = operDate+"-01 00:00:00";
			statisEndTime = operDate+"-31 23:59:59";
		}else if(operDate.length == 10){ // 按日汇总
			statisBeginTime = operDate+" 00:00:00";
			statisEndTime = operDate+" 23:59:59";
		}
		data.type=type;
        data.orgIds = orgIds;
        data.lineIds = lineId;
        data.statisBeginTime = statisBeginTime;
        data.statisEndTime = statisEndTime;
        data.spedType = spedType;
        data.lineId = lineId;
        $.openTab('82574146a7d34a1bb4423bafbeddbe21','showReportOverSpeedDetail',[data]);
    };
    //跳转到线路明细页面
    goLineInfo = function(lineId){
    	var data = {};
    	data.lineId = lineId;
    	$.openTab('4db17f8a322b4749a61eb2f8a12023ae','showLineInfo',[data]);
    }
    
    /*$('body').css('overflow', 'inherit');*/
});